package com.xcfw.dorm.config;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

/**
 * @program: Printer
 * @description: 拦截错误编号
 * @author: Bruse Queen
 * @create: 2018-03-15 16:36
 **/
@Configuration
public class ErrorConfiguration {
    /**
     * 定义错误代码拦截
     *
     * @return EmbeddedServletContainerCustomizer 框架相关规定容器
     */
    @Bean
    public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
                configurableEmbeddedServletContainer.addErrorPages(
                        new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
                configurableEmbeddedServletContainer.addErrorPages(
                        new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500"));
                configurableEmbeddedServletContainer.addErrorPages(
                        new ErrorPage(HttpStatus.NOT_FOUND, "/404"));
            }
        };
    }
}
